package com.ljfresh.customer.job;

import com.alibaba.fastjson.JSONObject;
import com.ljfresh.customer.erp.ERPBasicAPIUtil;
import com.ljfresh.customer.erp.ERPConsts;
import com.ljfresh.customer.pojo.Store;
import com.ljfresh.customer.result.StoreERPResult;
import com.ljfresh.customer.result.TokenResult;
import com.ljfresh.customer.sercice.StoreService;
import com.ljfresh.customer.sercice.TwiterIdService;
import com.ljfresh.customer.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * @author: Lucifer
 * @time: 2020/4/13-9:21
 */
@Component
public class StoreJob {

    @Resource
    private RedisUtils redisUtils;

    @Autowired
    private TwiterIdService twiterIdService;

    @Resource
    private StoreService storeService;

    //@Scheduled(cron = "0 0 1 * * ?")
    public void StoreSysnJob() throws Exception{
        TokenResult tokenResult = JSONObject.parseObject(redisUtils.get(ERPConsts.erpTokenRedisKey).toString(), TokenResult.class);
        if (tokenResult!=null){
            List<StoreERPResult> stores = ERPBasicAPIUtil.findStore(tokenResult.getRefresh_token());
            if (stores.size()>0){
                storeService.deleteAllStore();
                for (StoreERPResult storeERPResult:stores) {
                    Store store = new Store();
                    store.setId(twiterIdService.getTwiterId());
                    store.setStoreName(storeERPResult.getBranch_name());
                    store.setStoreCode(storeERPResult.getBranch_num());
                    storeService.save(store);
                }
            }
        }
    }
}
